/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */
package space.app.meshview.state;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import jinngine.math.Matrix3;
import jinngine.math.Quaternion;
import jinngine.math.Vector3;

/**
 *
 * @author Pierre
 */
class MoveState extends AbstractState {

    public static final double SENSIVITY = -2.;
    final MeshCamera camera;
    final Quaternion light;
    final Matrix3 inverseViewpoint;
    final int oldx, oldy;
    final Vector3 oldCenter;
    

    MoveState(MeshCamera camera, Quaternion light, MouseEvent e) {
        inverseViewpoint = new Matrix3(camera.viewpoint()).inverse();
        oldx = e.getX();
        oldy = e.getY();
        this.camera = camera;
        this.light = light;
        oldCenter = new Vector3(camera.center);
    }

    @Override
    public AbstractState mouseDragged(MouseEvent e) {
        final Canvas canvas = (Canvas) e.getSource();
        final Dimension dim = canvas.getSize();
        camera.center.assign(oldx - e.getX(), e.getY() - oldy, 0);
        camera.center.assignMultiply(SENSIVITY * camera.currentDistance / dim.getWidth());
        Matrix3.multiply(inverseViewpoint, camera.center, camera.center);
        camera.center.assignAdd(oldCenter);
        redraw(e);
        return this;
    }

    @Override
    public AbstractState mouseReleased(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON2) {
            return new DefaultState(camera, light);
        }
        return this;
    }
}
